//
//  ClassJokeTableViewCell.m
//  HaHa
//
//  Created by 姚君泽 on 15/9/18.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "ClassJokeTableViewCell.h"
#define kWidth self.commentLabel.frame.origin.y + self.commentLabel.frame.size.height + 10
@implementation ClassJokeTableViewCell


- (void)awakeFromNib {
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.userImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 40, 40)];
        _userImageView.layer.masksToBounds = YES;
        self.userImageView.layer.cornerRadius = _userImageView.height/2;
        [self addSubview:self.userImageView];
        
        self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 15, kScreenWdth - 130, 40)];
        
        [self addSubview:self.userNameLabel];
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, kScreenWdth - 40, 300)];
        
        [self addSubview:self.contentLabel];
        
//        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 370, 70, 30)];
//        
//        
//        [self addSubview:self.tagLabel];
        
//        self.commentView = [[UIView alloc] initWithFrame:CGRectMake(10, 420, kScreenWdth - 40, 200)];
//        self.commentView.backgroundColor = [UIColor whiteColor];
//        [self addSubview:self.commentView];
        
        self.observerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 20, 20)];
       
        [self addSubview:self.observerImageView];
        
        self.observerLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 70, 20)];
        
        [self addSubview:self.observerLabel];
        
        self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, kScreenWdth - 60, 130)];
        
        [self addSubview:self.commentLabel];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kScreenWdth - 60, 15, 40, 40);
        button.backgroundColor = [UIColor purpleColor];
        [self addSubview:button];
        self.goodButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.goodButton setImage:[UIImage imageNamed:@"praise"] forState:(UIControlStateNormal)];
        [self.goodButton addTarget:self action:@selector(ButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:self.goodButton];
        
        self.goodLabel = [[UILabel alloc] init];
        self.goodLabel.backgroundColor = [UIColor greenColor];
        self.value = arc4random()%10000;
        self.goodLabel.text = [NSString stringWithFormat:@"%d",self.value];
        [self addSubview:self.goodLabel];
        
        self.badButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.badButton setImage:[UIImage imageNamed:@"buried"] forState:(UIControlStateNormal)];
        [self addSubview:self.badButton];
        
        self.badLabel = [[UILabel alloc] init];
        self.badLabel.backgroundColor = [UIColor greenColor];
        [self addSubview:self.badLabel];
        
        self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.commentButton setImage:[UIImage imageNamed:@"Comment"] forState:(UIControlStateNormal)];
        [self addSubview:self.commentButton];
        
        self.commentNumLabel = [[UILabel alloc] init];
        self.commentNumLabel.backgroundColor = [UIColor blueColor];
        [self addSubview:self.commentNumLabel];
        
        self.getButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.getButton setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
        [self addSubview:self.getButton];
    }
    return self;
}
-(void)ButtonClick
{
    NSLog(@"点了一下");
    int temp = self.value;
    temp += 1;
    self.goodLabel.text = [NSString stringWithFormat:@"%d",temp];
}

//给cell赋值
-(void)setValueWithmodel:(WeekJokeModel *)model
{
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:model.essay_user[@"avatar_url"]]];
    self.userNameLabel.text = model.essay_user[@"name"];
    self.tagLabel.text = model.tag_name;
    self.contentLabel.text = model.text;
    [self setCellFrame];
}

-(void)SetValueWithModel:(todayJokeModel *)model
{
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:model.userDic[@"avatar_url"]]];
    self.userNameLabel.text = [model.userDic objectForKey:@"name"];
    self.tagLabel.text = model.category_name;
    self.contentLabel.text = model.content;
    [self setCellFrame];
}


-(void)setvalueWithModel:(ClassJokeModel *)model
{
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:model.user[@"avatar_url"]]];
    self.userNameLabel.text = model.user[@"name"];
    self.tagLabel.text = model.category_name;
    self.contentLabel.text = model.content;
    [self.observerImageView sd_setImageWithURL:[NSURL URLWithString:model.comment[@"avatar_url"]]];
    self.observerLabel.text = model.comment[@"user_name"];
    self.commentLabel.text = model.comment[@"text"];
    
    [self setCellFrame];
}


//设置cell的frame
-(void)setCellFrame
{
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [self.contentLabel sizeThatFits:CGSizeMake(self.contentLabel.frame.size.width, MAXFLOAT)];
    self.contentLabel.frame = CGRectMake(self.contentLabel.frame.origin.x, self.contentLabel.frame.origin.y, self.contentLabel.frame.size.width, size.height);
   
    self.observerImageView.frame =CGRectMake(10, self.contentLabel.frame.origin.y + size.height + 10, 20, 20);
    
    self.observerLabel.frame = CGRectMake(40, self.contentLabel.frame.origin.y + size.height + 10, 200, 20);
    
    self.commentLabel.numberOfLines = 0;
    
    self.commentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    CGSize comSize = [self.commentLabel sizeThatFits:CGSizeMake(self.commentLabel.frame.size.width, MAXFLOAT)];
    self.commentLabel.frame = CGRectMake(self.commentLabel.frame.origin.x, self.observerLabel.frame.origin.y + self.observerLabel.frame.size.height + 10, self.frame.size.width, comSize.height);
   
    if (self.commentLabel.frame.size.height == 0) {
        self.observerLabel.frame = CGRectMake(0, 0, 0, 0);
        self.observerImageView.frame = CGRectMake(0, 0, 0, 0);
    }
    self.goodButton.frame = CGRectMake(10, kWidth, 30, 30);
    self.goodLabel.frame = CGRectMake(40, kWidth, 40, 30);
    self.badButton.frame = CGRectMake(110, kWidth, 30, 30);
    self.badLabel.frame = CGRectMake(140, kWidth, 40, 30);
    self.commentButton.frame = CGRectMake(210, kWidth, 30, 30);
    self.commentNumLabel.frame = CGRectMake(240, kWidth, 40, 30);
    self.getButton.frame = CGRectMake(310, kWidth, 30, 30);
    
//    self.tagLabel.frame = CGRectMake(10, self.contentLabel.frame.origin.y + size.height + 10, 70, 30);
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
